{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 装饰器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T11:23:25.486800Z",
     "start_time": "2021-11-24T11:23:24.482800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是func1\n"
     ]
    }
   ],
   "source": [
    "def func1():\n",
    "    time.sleep(1)\n",
    "    print('我是func1')\n",
    "func1()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T11:30:13.488800Z",
     "start_time": "2021-11-24T11:30:13.485800Z"
    }
   },
   "outputs": [],
   "source": [
    "def timer_it(func):\n",
    "    import time\n",
    "    def wrapper():\n",
    "        start = time.time()\n",
    "        func()\n",
    "        end = time.time()\n",
    "        cost_time = end - start\n",
    "        print('func cost {time} seconds'.format(time = cost_time))\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T11:30:18.864800Z",
     "start_time": "2021-11-24T11:30:15.860800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是func1\n",
      "func cost 3.0 seconds\n"
     ]
    }
   ],
   "source": [
    "@timer_it\n",
    "def func1():\n",
    "    time.sleep(3)\n",
    "    print('我是func1')\n",
    "    \n",
    "func1()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 装饰器接收参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 接收普通参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T11:35:29.782800Z",
     "start_time": "2021-11-24T11:35:29.778800Z"
    }
   },
   "outputs": [],
   "source": [
    "def timer_it(func):\n",
    "    import time\n",
    "    def wrapper(*args):\n",
    "        start = time.time()\n",
    "        func(*args)\n",
    "        end = time.time()\n",
    "        cost_time = end - start\n",
    "        print('func cost {time} seconds'.format(time = cost_time))\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T11:35:40.242800Z",
     "start_time": "2021-11-24T11:35:39.238800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果是3\n",
      "func cost 1.0 seconds\n"
     ]
    }
   ],
   "source": [
    "@timer_it\n",
    "def func2(x,y):\n",
    "    z = x + y\n",
    "    time.sleep(1)\n",
    "    print('结果是{}'.format(z))\n",
    "\n",
    "func2(1,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 接收键值对参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T11:38:35.037800Z",
     "start_time": "2021-11-24T11:38:35.032800Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T11:40:14.359800Z",
     "start_time": "2021-11-24T11:40:14.355800Z"
    }
   },
   "outputs": [],
   "source": [
    "def timer_it(func):\n",
    "    import time\n",
    "    def wrapper(*args, **kwargs):\n",
    "        start = time.time()\n",
    "        func(*args,**kwargs)\n",
    "        end = time.time()\n",
    "        cost_time = end - start\n",
    "        print('func cost {time} seconds'.format(time = cost_time))\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T11:40:22.910800Z",
     "start_time": "2021-11-24T11:40:21.906800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果是7\n",
      "func cost 1.0 seconds\n"
     ]
    }
   ],
   "source": [
    "@timer_it\n",
    "def func2(x = None ,y = None):\n",
    "    z = x + y\n",
    "    time.sleep(1)\n",
    "    print('结果是{}'.format(z))\n",
    "    \n",
    "func2(x = 3, y = 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本身带参数的装饰器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-01T10:53:43.813604Z",
     "start_time": "2021-12-01T10:53:43.807604Z"
    }
   },
   "outputs": [],
   "source": [
    "def log_it(msg=None):\n",
    "    def timer_it(func):\n",
    "        import time\n",
    "        def wrapper(*args, **kwargs):\n",
    "            start = time.time()\n",
    "            func(*args, **kwargs)\n",
    "            end = time.time()\n",
    "            cost_time = end - start\n",
    "            print('{msg} cost {time} seconds'.format(msg = msg,time = cost_time))\n",
    "        return wrapper\n",
    "    return timer_it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-01T10:53:58.294604Z",
     "start_time": "2021-12-01T10:53:57.290604Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是func3\n",
      "func3 cost 1.0 seconds\n"
     ]
    }
   ],
   "source": [
    "import time \n",
    "\n",
    "@log_it(msg = 'func3')\n",
    "def func3():\n",
    "    time.sleep(1)\n",
    "    print('我是func3')\n",
    "func3()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "课后练习2：写一个装饰器，可以用txt文件（文件名由用户输入）记录下函数的输入参数，输出结果，函数开始运行的时间：\n",
    "\n",
    "函数名：func1\n",
    "输入参数：(1, 2)\n",
    "输出结果：3\n",
    "开始运行时间：1637764225.328604"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T14:30:22.631604Z",
     "start_time": "2021-11-24T14:30:22.625604Z"
    }
   },
   "outputs": [],
   "source": [
    "def write_it(file=None):\n",
    "    import time\n",
    "    def log_it(func):\n",
    "        def wrapper(*args, **kwargs):\n",
    "            start = time.time()\n",
    "            with open(file,'w') as f:\n",
    "                f.write('函数名：{}'.format(func.__name__))\n",
    "                f.write('\\n')\n",
    "                f.write('输入参数：{} {}'.format(args,kwargs))\n",
    "                f.write('\\n')\n",
    "                f.write('输出结果：{}'.format(func(*args,**kwargs)))\n",
    "                f.write('\\n')\n",
    "                f.write('开始运行时间：{}'.format(start))\n",
    "                f.write('\\n')\n",
    "            return func(*args,**kwargs)\n",
    "        return wrapper\n",
    "    return log_it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T14:30:25.340604Z",
     "start_time": "2021-11-24T14:30:25.327604Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@write_it(file = '123.txt')\n",
    "def func1(x,y):\n",
    "    return x+y\n",
    "\n",
    "func1(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
